import { pageInstructReceipt } from "../../../service/api/api-instruct-record";

Component({
  properties: {
    recordId: String,
    type: Number,
    createdCombineId: String,
    reload: Boolean
  },

  data: {
    dataList: [],
    loading: false,
    currentPage: 1,
    pageSize: 20,
    hasMore: true,
    search: ''
  },
 
  methods: {
    loadData() {
      this.setData({ loading: true });

      const params = {
        recordId: this.properties.recordId,
        type: this.properties.type,
        current: this.data.currentPage,
        pageSize: this.data.pageSize,
        search:this.data.search
      };

      this.loadCuesTopicDetails(params)
    },
    async loadCuesTopicDetails(params) {
      try {
        const response = await pageInstructReceipt(params);
        this.setData({ loading: false });
        if (response.code === 0) {
          console.log(response.data.data);
          
          this.setData({
            dataList: response.data.data,
            hasMore: response.data.total > this.data.currentPage * this.data.pageSize
          });
        }
      } catch (error) { }
    },
    downloadFile(e) {
      const url = e.currentTarget.dataset.url;
      wx.downloadFile({
        url: url,
        success(res) {
          const filePath = res.tempFilePath;
          wx.openDocument({
            filePath: filePath,
            showMenu: true
          });
        }
      });
    },

    prevPage() {
      if (this.data.currentPage > 1) {
        this.setData({
          currentPage: this.data.currentPage - 1
        }, this.loadData);
      }
    },

    nextPage() {
      if (this.data.hasMore) {
        this.setData({
          currentPage: this.data.currentPage + 1
        }, this.loadData);
      }
    },

    onSearch(e) {
      this.setData({
        search: e.detail.value,
        currentPage: 1
      }, this.loadData);
    }
  },

  lifetimes: {
    attached() {
      this.loadData();
    }
  },

  observers: {
    'reload': function (reload) {
      console.log(reload);
      
      if (reload) {
        this.loadData();
        this.setData({ reload: false });
      }
    },
    'createdCombineId': function (id) {
      if (id) {
        this.loadData();
      }
    }
  }
});